import 'dart:ffi';
import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:test01/okhttp_test/page/pages/okhttp_main.dart';
import 'package:test01/okhttp_test/pojo/attention_pojo.dart';
import 'package:test01/okhttp_test/util/request_http.dart';

/*
*
*
* 画布----布局
*
* */

//画布Y坐标轴
class MyPainterX extends CustomPainter {
  MyPainterX(this.getRecordLists,this.scaleWeight,this.targetWeight);

  //获取接口数据
  List<GetRecordLists> getRecordLists;
  //获取目标体重
  double targetWeight;
  //获取Y轴的最大刻度尺
  double scaleWeight;

  @override
  void paint(Canvas canvas, Size size) {

    ///床架画笔
    var paint = Paint()
      ..strokeWidth = 1
      ..isAntiAlias = true // 是否抗锯齿
      ..color = Color.fromRGBO(153, 153, 153, 1);

    ///创建背景虚线
    var max = size.width - 16; // size获取到宽度
    //虚线之间的宽度
    var dashWidth = 5;
    var dashSpace = 5;
    //虚线X轴的初始值
    double startX = 37.5;
    //虚线循环之间的宽度
    final space = (dashSpace + dashWidth);

    //虚线显示
    while (startX < max) {
      for (var i = 0; i < 5; i++) {
        canvas.drawLine(Offset(startX, (i + 1) * 224 / 5),
            Offset(startX + dashWidth, (i + 1) * 224 / 5), paint);
      }
      startX += space;
    }
    ///绘制目标体重的虚线
    if(targetWeight!=null&&scaleWeight!=null){
      //绘制目标体重的虚线
      var paints = Paint()
        ..strokeWidth = 1
        ..isAntiAlias = true // 是否抗锯齿
        ..color = Color.fromRGBO(255, 64, 89, 1);

      //虚线显示
      var targetStartX=37.5;
      while (targetStartX < max) {
        canvas.drawLine(Offset(targetStartX, 224-(((((224/5)*5)-(224/5))/scaleWeight) * targetWeight)),
            Offset(targetStartX + dashWidth,224-(((((224/5)*5)-(224/5))/scaleWeight) * targetWeight)), paints);
        print("=======================$startX===================");
        targetStartX += space;
      }
      var textPainter = TextPainter(
        text: TextSpan(
            text: "目标${targetWeight}",
            style: TextStyle(
                color: Color.fromRGBO(255, 64, 89, 1), fontSize:9)),
        textDirection: TextDirection.rtl,
      )..layout();
      textPainter.paint(canvas, Offset(startX-21-21, (224-(((((224/5)*5)-(224/5))/scaleWeight) * targetWeight))-12));
    }

    ///初始值
    var startOffset = 20.5 - 16;
    ///Y轴
    for (int i = 0; i < 5; i++) {
      //绘制文字
      var textPainter = TextPainter(
        text: TextSpan(
            text: "${(scaleWeight/ 4) * i}",
            style: TextStyle(
                color: Color.fromRGBO(153, 153, 153, 1), fontSize: 12)),
        textDirection: TextDirection.rtl,
      )..layout();
      textPainter.paint(canvas, Offset(startOffset, ((5 - i) * (224 / 5)) - 7));
    }
  }

  ///是否进行界面的重新刷新加载
  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

//画布X坐标轴
class MyPainterY extends CustomPainter{
  MyPainterY(this.getRecordLists, this.context,this.mm,this.scaleWeight);

  ///获取接口数据
  List<GetRecordLists> getRecordLists;
  BuildContext context;
  int mm;
  double scaleWeight;

  ///初始化折线坐标列表
  List<Offset> points = [];

  ///初始化起始坐标
  double startX = 37.5;

  ///初始化折线点
  var brokenLine = Paint()
    ..strokeWidth = 1.5
    ..isAntiAlias = true // 是否抗锯齿
    ..style = PaintingStyle.stroke
    ..color = Color.fromRGBO(255, 64, 89, 1);

  ///初始化两个画布
  var path1 = Path();
  var path = Path();

  ///初始值
  var startOffset = 20.5 - 16;

  @override
  void paint(Canvas canvas, Size size) {
    ///X轴
    if(scaleWeight!=null){
      for (int i = getRecordLists.length - 1; i >= 0; i--) {
        ///绘制X轴的文字显示
        List<String> zz = getRecordLists[i].dateTime.split("-");
        // 字符串截取
        // if(zz[1].startsWith("0")){
        //   zz[1]=zz[1].substring(1, 2);
        // }
        //绘制文字
        var textPainter = TextPainter(
          text: TextSpan(
              text: "${zz[1]}.${zz[2]}",
              style: TextStyle(
                  color: Color.fromRGBO(153, 153, 153, 1), fontSize: 12)),
          textDirection: TextDirection.rtl,
        )..layout();

        ///X轴
        textPainter.paint(
            canvas,
            Offset(
                (((MediaQuery.of(context).size.width - 50 - 18) / 5) *
                    (getRecordLists.length - i - 1)) ,
                5 * (224 / 5) + 22));
        /// 绘制折线显示
        if (i == getRecordLists.length - 1) {
          path1.moveTo(
              (((MediaQuery.of(context).size.width - 50 - 2) / 5) *
                  (getRecordLists.length - i - 1)) +
                  12,
              224-(((((224/5)*5)-(224/5))/scaleWeight) * (getRecordLists[i].weight))
          );
        } else {
          path1.lineTo(
              (((MediaQuery.of(context).size.width - 50 - 2) / 5) *
                  (getRecordLists.length - i - 1)) +
                  12,
              224-(((((224/5)*5)-(224/5))/scaleWeight) * (getRecordLists[i].weight))
          );
        }
        ///绘制圆点
        points.add(Offset(
            (((MediaQuery.of(context).size.width - 50 - 2) / 5) *
                (getRecordLists.length - i - 1)) +
                12,
            224-(((((224/5)*5)-(224/5))/scaleWeight) * (getRecordLists[i].weight)))
        );

        ///绘制背景图片
        if (i == getRecordLists.length - 1) {
          path.moveTo(12, 5 * (224 / 5) + 22);
          path.lineTo(12, 224-(((((224/5)*5)-(224/5))/scaleWeight) * (getRecordLists[i].weight)));
        } else if (i == 0) {
          path.lineTo(
              (((MediaQuery.of(context).size.width - 50 - 2) / 5) *
                  (getRecordLists.length - 0 - 1)) +
                  12,
              224-(((((224/5)*5)-(224/5))/scaleWeight) * (getRecordLists[i].weight)));
          path.lineTo(
              (((MediaQuery.of(context).size.width - 50 - 2) / 5) *
                  (getRecordLists.length - 0 - 1)) +
                  12,
              5 * (224 / 5) + 22);
        } else {
          path.lineTo(
              (((MediaQuery.of(context).size.width - 50 - 2) / 5) *
                  (getRecordLists.length - i - 1)) +
                  12,
              224-(((((224/5)*5)-(224/5))/scaleWeight) * (getRecordLists[i].weight)));
        }
      }
    }
    canvas.drawPath(path1, brokenLine);

    ///绘制背景色渐变
    //颜色渐变
    final rect = new Rect.fromLTWH(0.0, 0.0, size.width, size.height - 20);
    var gradient = new LinearGradient(
      begin: Alignment.topCenter,
      end: Alignment.bottomCenter,
      colors: [
        Color.fromRGBO(255, 64, 89, 1),
        Color.fromRGBO(255, 255, 255, 0.5)
      ],
    );
    //点
    var _paint2 = Paint()
      ..strokeWidth = 1.5
      ..isAntiAlias = true // 是否抗锯齿
      // ..style = PaintingStyle.stroke
      ..color = Colors.red
      ..shader = gradient.createShader(rect);

    ///绘制到画布
    canvas.drawPath(path, _paint2);

    ///绘制圆点
    //圆点
    var _paints = Paint()
      ..strokeWidth = 1.5
      ..isAntiAlias = true // 是否抗锯齿
      ..style = PaintingStyle.stroke
      ..color = Color.fromRGBO(255, 64, 89, 1);
    //圆点
    var _paints2 = Paint()
      ..strokeWidth = 1.5
      ..isAntiAlias = true // 是否抗锯齿

      ..color = Colors.white;

    for (int i = 0; i < points.length; i++) {
        canvas.drawCircle(points[i], 3.0, _paints2 //绘画风格改为stroke
        );
        canvas.drawCircle(points[i], 3.0, _paints //绘画风格改为stroke
        );
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

//点击的坐标
class MyChangeY extends CustomPainter {
  MyChangeY(this.getRecordLists, this.context,this.mm,this.scaleWeight);

  ///获取接口数据
  List<GetRecordLists> getRecordLists;
  BuildContext context;
  int mm;
  double scaleWeight;

  ///初始化折线坐标列表
  List<Offset> points = [];



  @override
  void paint(Canvas canvas, Size size) {
    print("---------------------调用了--------------------");

    ///绘制圆点
    //圆点
    var _paints = Paint()
      ..strokeWidth = 1.5
      ..isAntiAlias = true // 是否抗锯齿
      ..color = Color.fromRGBO(255, 64, 89, 1);
    //圆点
    var _paints2 = Paint()
      ..strokeWidth = 1.5
      ..isAntiAlias = true // 是否抗锯齿
      ..style = PaintingStyle.stroke
      ..color = Colors.white;

    //X轴的初始值
    double changeX =(((MediaQuery.of(context).size.width - 50 - 2) / 5) *
        (getRecordLists.length - mm - 1)) +
        12;

    //X轴的初始值
    double changeY =224-(((((224/5)*5)-(224/5))/scaleWeight) * (getRecordLists[mm].weight));

    //虚线之间的宽度
    var dashWidth = 5;
    var dashSpace = 5;
    //虚线循环之间的宽度
    final space = (dashSpace + dashWidth);
    var starY=224/5;

    //虚线显示
    while (starY <= ((224/5)*5)) {
      for (var i = 0; i < 5; i++) {
        canvas.drawLine(Offset(changeX,starY),
            Offset(changeX ,starY+dashWidth), _paints);
      }
      starY += space;
    }
    ///绘制圆点
    points.add(Offset(
      changeX,changeY
        )
    );
      canvas.drawCircle(points[0], 3.0, _paints2 //绘画风格改为stroke
      );
      canvas.drawCircle(points[0], 3.0, _paints //绘画风格改为stroke
      );
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

